import {
  getRankingBooks,
  getRecommendedBooksByUserId,
  getBooksByKeyword,
  getBooksByAuthor,
  getBooksByTag,
  getBooksByAdvancedSearch,
  getBooksByClassificationNumber
} from '../../apis/book'
import {
  getUID
} from '../../utils/permission'
import {
  handleBookData
} from '../common/common';
var options = {} // 保存 options 参数

Page({
  data: {
    type: {},
    books: [],
    comments: [], // 推荐图书的描述
    loadMoreStatus: 'hidding', // loading, nomore
    isNoData: false // 是否没有数据
  },

  onLoad: function(opt) {
    Object.keys(opt).forEach(key => {
      options[key] = decodeURIComponent(opt[key])
    })
    this.setData({
      type: options.type
    })
    switch (options.type) {
      case 'search':
      case 'advanced_search':
      case 'classify_search':
        wx.setNavigationBarTitle({
          title: '搜索结果'
        })
        break
      case 'recommend':
        wx.setNavigationBarTitle({
          title: '推荐图书'
        })
        break
      case 'ranking':
        wx.setNavigationBarTitle({
          title: '近期热门图书'
        })
        break
    }

    wx.showLoading({
      title: '加载中',
      mask: true
    })
    this._fetchData().then(data => {
      console.log('dd',data);
      // 第一次加载数据时判断是否“暂无数据”
      if (data.total === 0 || (Array.isArray(data) && data.length === 0)) {
        this.setData({
          isNoData: true
        })
      }
    }).finally(() => wx.hideLoading())
  },

  onReachBottom: function() {
    let {
      loadMoreStatus,
      isNoData
    } = this.data

    // 推荐图书一次性即可加载完毕，不再加载
    if (loadMoreStatus !== 'hidding' || isNoData || options.type === 'recommend') return

    this.setData({
      loadMoreStatus: 'loading'
    })
    this._fetchData(this.data.books.length).then(data => {
      !data.hasNextPage || options.type === 'recommend' ?
        this.setData({
          loadMoreStatus: 'nomore'
        }) :
        this.setData({
          loadMoreStatus: 'hidding'
        })
    }).catch(() => this.setData({
      'loadMoreStatus': 'hidding'
    }))
  },

  /**
   * 获取图书数据
   * @param start {Integer} 搜索偏移量
   */
  _fetchData: function(start = 0) {
    let fn
    switch (options.type) {
      case 'search':
        if (options.search_type === '书名') {
          fn = getBooksByKeyword(options.keyword, start)
        } else if (options.search_type === '作者') {
          fn = getBooksByAuthor(options.keyword, start)
        } else if (options.search_type === '标签') {
          fn = getBooksByTag(options.keyword, start)
        }
        break
      case 'advanced_search':
        options['start'] = start
        fn = getBooksByAdvancedSearch(options)
        break
      case 'classify_search':
        fn = getBooksByClassificationNumber(options.class_num, start)
        break
      case 'recommend':
        fn = getRecommendedBooksByUserId(getUID())
        break
      case 'ranking':
        fn = getRankingBooks(start)
        break
      default:
        throw new Error('不支持的搜索类型！')
    }

    return fn.then(res => {

      let books ;;
      if (options.type === 'recommend') {
        console.log("books", books);
        /* 推荐图书接口的返回值为
         * {
         *   books: [{
         *     book: Book,
         *     comment: String
         *   }],
         *   total: Integer
         * }
         */
        books = res.data.map(item => handleBookData(item));
        this.setData({
          books: books,
          comments: [],
          // comments: res.data.map(e => e.comment)
        })
      } else {
        /* 其他接口的返回值为
         * {
         *   books: [Book],
         *   total: Integer
         * }
         */
        books=res.data.list.map(item => handleBookData(item));
        this.setData({
          books: this.data.books.concat(books)
        })
      }
      return res.data;
    }).catch(() => {
      return []
    })
  }
})